﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Warensoft.Unity.Communication.Client.Models;

namespace Warensoft.Unity.Communication.Client.Internals
{
    public class HttpConnectionPool
    {
        private int maxPoolSize;

        public int MaxPoolSize
        {
            get { return maxPoolSize; }
            set { maxPoolSize = value; }
        }
        private List<HttpRequest> httpRequests;

       

        public HttpConnectionPool(int maxPoolSize)
        {
            this.maxPoolSize = maxPoolSize;
        }
        public HttpRequest GetRequest(string url)
        {
            HttpRequest request = null;
            request = this.httpRequests.FirstOrDefault(r=>r.IsSendingMessage ==false );
            if (request !=null )
            {
                request.Url = url;
                return request;
            }
            if (this.httpRequests .Count <this.maxPoolSize )
            {
                request= new HttpRequest() { Url =url };
                this.httpRequests.Add(request);
                return request;
            }
            return null;
        }
    }
}
